package com.xiaomi.baidumap.github;

import android.util.Base64;

import com.xiaomi.baidumap.common.LogUtils;
import com.xiaomi.baidumap.model.Repo;
import com.xiaomi.baidumap.model.RepoContentResult;
import com.xiaomi.baidumap.network.GithubClient;

import java.io.IOException;

import okhttp3.MediaType;


import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 16-8-30.
 */

/*
*主要进行业务的处理
* 1.获取readme文件
* 2.对文件进行解密
* 3.获取文件的内容
* */
public class RepoInfoPresenter {

    private RepoInfoView mRepoInfoView;
    //创建一个接口，完成视图
    public interface RepoInfoView{
        /*
        * 1.隐藏进度条
        * 2.显示进度条
        * 3.显示信息
        * 4.加载完数据，显示数据
        * */

        void showProgress();
        void hideProgress();
        void showMessager(String msg);
        void setData(String htmlContent);
    }

    public RepoInfoPresenter(RepoInfoView repoInfoView) {
        mRepoInfoView = repoInfoView;
    }

    private Call<RepoContentResult> repoContentCall;
    private Call<ResponseBody> responseBodyCall;

    public void getReadme(Repo repo){
        //显示进度条,调用本类中的接口
        mRepoInfoView.showProgress();
        String login = repo.getOwner().getLogin();
        String name = repo.getName();
        if (repoContentCall != null){
            repoContentCall.cancel();
        }
        repoContentCall = GithubClient.getInstance().getReadme(login,name);
        LogUtils.d("333");
        //进行异步操作
        repoContentCall.enqueue(repoContentResultCallback);
    }

    private Callback<RepoContentResult> repoContentResultCallback = new Callback<RepoContentResult>() {



        @Override
        public void onResponse(Call<RepoContentResult> call, Response<RepoContentResult> response) {
            //获得内容
            String content = response.body().getContent();
            LogUtils.d(content);
            //然后进行解密然后获得内容
            byte[] decode = Base64.decode(content, Base64.DEFAULT);
            //创建媒体类型
            MediaType mediaType = MediaType.parse("text/plain");
            RequestBody requestBody = RequestBody.create(mediaType, decode);
            responseBodyCall = GithubClient.getInstance().markDown(requestBody);
            LogUtils.d("222");
            //进行异步处理
            responseBodyCall.enqueue(responseBodyCallback);
        }

        @Override
        public void onFailure(Call<RepoContentResult> call, Throwable t) {
            mRepoInfoView.hideProgress();
            mRepoInfoView.showMessager(t.getMessage());
        }
    };

    private Callback<ResponseBody> responseBodyCallback = new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

            //显示数据，要隐藏进度条
            mRepoInfoView.hideProgress();
            try {
                //获取数据
                String htmlContent = response.body().string();
                LogUtils.d(htmlContent);
                //设置数据
                mRepoInfoView.setData(htmlContent);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            mRepoInfoView.hideProgress();
            mRepoInfoView.showMessager(t.getMessage());
        }
    };

}
